package leecode

import "sort"

func deleteDuplicates2(head *ListNode) *ListNode {
	if head == nil {
		return nil
	}
	hashMap := make(map[int]int)
	for head != nil {
		hashMap[head.Val]++
		head = head.Next
	}
	arr := make([]int, 0, len(hashMap))
	for key, value := range hashMap {
		if value == 1 {
			arr = append(arr, key)
		}
	}
	if len(arr) <= 0 {
		return nil
	}
	sort.Ints(arr)
	result := &ListNode{Val: arr[0]}
	dummyHead := result
	for i := 1; i < len(arr); i++ {
		dummyHead.Next = &ListNode{Val: arr[i]}
		dummyHead = dummyHead.Next
	}
	return result
}
